package com.keshan.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.keshan.pojo.*;
import com.keshan.service.StepCountServices;
import com.keshan.service.UserDeviceServices;
import com.keshan.utils.DateUtil;
import com.keshan.utils.JWTUtils;
import com.keshan.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author keshan
 * @date 2022/10/21 - 19:48
 */
@CrossOrigin
@Controller
@RequestMapping("/stepcount")
public class StepCountController {
    @Autowired
    StepCountServices stepCountServices;
    @Autowired
    UserDeviceServices userDeviceServices;

    @RequestMapping("/insertstepcount")
    @ResponseBody
    public Msg insertBySelectiveToStepCount(@RequestBody StepCountJson stepCountJson) {
        Date date = new Date();
        System.out.println(stepCountJson.toString());
        System.out.println(date.toString());
        List<UserDevice> userDeviceList = userDeviceServices.getUserDeviceListByImei(stepCountJson.getImei());
        try {
            StepCount stepCount = new StepCount();
            stepCount.setDeviceType(stepCountJson.getDevice_type());
            stepCount.setUsername(userDeviceList.get(0).getUsername());
            stepCount.setTimeBegin(DateUtil.StringToDate(stepCountJson.getTime_begin_str()));
            stepCount.setValue(stepCountJson.getValue());
            System.out.println(stepCount.toString());
            int result = stepCountServices.insertBySelectiveStepCount(stepCount);
            return Msg.success().add("result", result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Msg.failInsert();
    }

    @RequestMapping("/stepcountlisthidetoday")
    @ResponseBody
    public Msg selectStepCountListHideByImeiToday(HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader("token");
        System.out.println(token);
        try {
            DecodedJWT decodedJWT = JWTUtils.verifyAndInfo(token);
            String username = decodedJWT.getClaim("username").asString();
            List<StepCountHide> stepCountList = stepCountServices
                    .getStepCountListHideByUsernameToday(username);
            return Msg.success().add("stepCountList", stepCountList);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return Msg.fail().add("error", "查询失败");
    }

    @RequestMapping("/stepcountlisteveryday")
    @ResponseBody
    public Msg selectStepCountEverydayListByImeiDayAvg(HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader("token");
        System.out.println(token);
        try {
            DecodedJWT decodedJWT = JWTUtils.verifyAndInfo(token);
            String username = decodedJWT.getClaim("username").asString();
            List<StepCountEveryday> stepCountEverydayList = stepCountServices
                    .getStepCountEverydayListEveryday(username);
            return Msg.success().add("stepCountList", stepCountEverydayList);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return Msg.fail().add("error", "查询失败");
    }

}
